/**
 * Created by fuqiang on 2017/5/24.
 */
import {Component, Input, OnInit} from '@angular/core';
import {ActivatedRoute, Params}   from '@angular/router';
import {Location}                 from '@angular/common';
import 'rxjs/add/operator/switchMap';
import {HeroService} from '../hero.service';
import {Hero} from '../hero';
@Component({
  selector: 'hero-detail',
  templateUrl: './detail.html',
  styleUrls: ['./detail.component.css']
})
export class HeroDetailComponent implements OnInit {
  @Input() hero: Hero;

  constructor(private heroService: HeroService,
              private route: ActivatedRoute,
              private location: Location) {
  }


  ngOnInit(): void {
    this.route.params
      .switchMap((params: Params) => this.heroService.getHero(+params['id']))
      .subscribe(hero => this.hero = hero);
  }

  // 后退方法
  goBack(): void {
    this.location.back();
  }

//  添加方法
  save(): void {
    this.heroService.update(this.hero)
      .then(() => this.goBack());
  }
}
